package cn.yitun.chat.yitunservermanagecore.general.manage.core.service.impl;

import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.Constants;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.group.GroupCategoryDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.*;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.*;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupCategoryDefaultEnums;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupMemberPositionEnums;
import cn.yitun.chat.yitunservercommonlib.enums.userChat.UserHeadTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimbase.dao.group.*;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.service.GroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 描述：
 * 
 * @author yitun
 * @date 2014年3月31日 上午11:45:15 version 0.0.1
 */
@Service
@Transactional
public class GroupServiceImpl
		extends CrudServiceImpl<GroupDao,GroupDto, GroupInfo, GroupVo>
		implements GroupService {

	@Resource
	private GroupMemberDao groupMemberDAO;
	@Resource
	private GroupCategoryDao groupCategoryDAO;
	@Resource
	private GroupCategoryMemberDao groupCategoryMemberDAO;
	@Resource
	private GroupNumberDao numberDAO;

	@Resource
	private	GroupHeadDao groupHeadDao;
//	@Resource
//	private GroupPush groupPush;
	@Override
	public GroupInfo getGroupById(Long id) {
		GroupInfo group = dao.get(id);
		return group;
	}
	@Override
	public GroupVo getVo(Long id) {
		GroupVo group = dao.getVO(id);
		return group;
	}
	@Override
	public GroupVo getGroupByCode(String code) {
		GroupVo group = dao.getGroupByCode(code);
		return group;
	}
	@Override
	public List<GroupCategoryVo> getGroupCategoryListByUserId(Long userId) {
		List<GroupCategoryVo> list = groupCategoryDAO.listVO(R.param("userId",userId));
		return list;
	}
	@Override
	public List<GroupRelationVo> getGroupCategoryMemberListByUserId(Long userId) {
		List<GroupRelationVo> list = groupCategoryMemberDAO.listVO(R.param("userId",userId) );
		return list;
	}
	@Override
	public List<GroupVo> getGroupCategoryMemberGroupListByUserId(Long userId) {
		List<GroupVo> list = dao.getGroupCategoryMemberGroupListByUserId(userId);
		return list;
	}
	@Override
	public List<GroupMemberVo> getGroupMemberListByGroupId(Long groupId) {
		List<GroupMemberVo> list = groupMemberDAO.listVO(R.param("groupId", groupId));
		return list;
	}
	@Override
	public List<GroupVo> queryGroupList(GroupDto groupQuery) {
		List<GroupVo> groupList = dao.listVO(YitunMapUtil.java2Map(groupQuery,false));
		return groupList;
	}

	/**
	 * 保存群
	 * @param entity
	 * @return
	 */
	@Transactional
	@Override
	public int save(GroupDto entity) {
		GroupNumberDto numberDto = new GroupNumberDto();
		numberDto.setCode(IdGen.uuid());
		numberDAO.save(numberDto);

		entity.setCode(numberDto.getCode());

		int count = dao.save(entity);
		//插入群主记录
		if (count > 0) {
			GroupMemberDto dto = new GroupMemberDto();
			dto.setUserId(entity.getCreateBy());
			dto.setGroupId(entity.getId());
			dto.setPosition(GroupMemberPositionEnums.POSITION_OWNER.getType());
			dto.setNickname(entity.getOwnerNickname());
			dto.setCreateBy(entity.getCreateBy());
			dto.setUpdateBy(entity.getCreateBy());
			groupMemberDAO.save(dto);
		}

		return count;
	}
	/**
	 * 保存群
	 * @param entity
	 * @return
	 */
	@Transactional
	@Override
	public int update(GroupDto entity) {

		int count = dao.update(entity);
		//插入群主记录
		if (count > 0)
			saveGroupHead(entity.getGroupHead(), entity.getId());

		return count;
	}

	/**
	 * 保存群头像
	 * @param newHead
	 * @param groupId
	 */
	@Transactional
	public void saveGroupHead(GroupHeadDto newHead, Long groupId) {
		if (newHead != null) {
			if (groupHeadDao.count(R.param("groupId", groupId)) > 0)
				groupHeadDao.removeByGroupId(groupId);

			newHead.setGroupId(groupId);
			newHead.setCreateBy(newHead.getUpdateBy());
			newHead.setUpdateBy(newHead.getUpdateBy());
			if (YitunStringUtil.isEmpty(newHead.getType()))
				newHead.setType(UserHeadTypeEnums.TYPE_SYSTEM.getType());

			groupHeadDao.save(newHead);
		}
	}

	@Override
	public ResultMessage add(Long key, Long userId, GroupDto group, GroupRelationDto groupCategoryMember) {

		GroupNumberDto groupNumber = new GroupNumberDto();// 生成数子账号
		groupNumber.setCode(IdGen.uuid());
		numberDAO.save(groupNumber);

		group.setCode(groupNumber.getCode());
		if (null == group.getHead() || "".equals(group.getHead())) {
			long i = Constants.baseRandomLong + new Random().nextLong();
			i = i + 1;
			group.setHead(i);
		}
		dao.save(group);

		if (null == groupCategoryMember) {
			groupCategoryMember = new GroupRelationDto();
		}
		groupCategoryMember.setUserId(userId);
		groupCategoryMember.setGroupId(group.getId());
		if (null == groupCategoryMember.getCategoryId() ) {
			GroupCategoryVo groupCategory = getOrAddDefaultGroupCategory(userId);
			groupCategoryMember.setCategoryId(groupCategory.getId());
		}

		groupCategoryMemberDAO.save(groupCategoryMember);

		GroupMemberDto groupMember = new GroupMemberDto();
		groupMember.setUserId(userId);
		groupMember.setGroupId(group.getId());
		groupMember.setPosition(GroupMemberPositionEnums.POSITION_OWNER.getType());
		groupMemberDAO.save(groupMember);

		ResultMessage message = new ResultMessage();
		message.put("group", group);
		message.put("groupCategoryMember", groupCategoryMember);
		message.put("groupMember", groupMember);
		
		List<Long> userIdList = new ArrayList<>();
		userIdList.add(userId);
		//groupPush.addGroup(urlPath, group, groupCategoryMember, groupMember, userIdList);
		return message;
	}

	private GroupCategoryVo getOrAddDefaultGroupCategory(Long userId) {
		GroupCategoryVo groupCategory = groupCategoryDAO.getDefaultGroupCategory(userId);
		if (null == groupCategory) {
			groupCategory = new GroupCategoryVo();// 生成默认分组信息
			groupCategory.setUserId(userId);
			groupCategory.setName("我的聊天群");
			groupCategory.setSort(GroupCategoryDefaultEnums.SORT_DEFAULT.getType());
			groupCategoryDAO.save(GroupCategoryDto2VoConverter.INSTANCE.vo2dto(groupCategory));
		}
		return groupCategory;
	}

	
	/**
	 * 获取用户在各个群中的权限
	 * @author yitun
	 * @date 2017年6月8日 下午3:32:39
	 * @param userId
	 * @return
	 */
	@Override
	public List<GroupMemberVo> getUserInGroupMemberList(Long userId) {
		List<GroupMemberVo> list = groupMemberDAO.listVO(R.param("userId", userId));
		return list;
	}
}
